FROM node:10.13-slim AS build

WORKDIR /src
COPY . .
#ENV HOME=/tmp
ARG BUILD_TYPE
RUN npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/ \
    && npm config set registry https://registry.npm.taobao.org \
    && npm install --skip-installed \
    && npm install -g supervisor \
    && npm run build-${BUILD_TYPE:-dev} \
    && npm run cos-upload-file-${BUILD_TYPE:-dev} \
    && npm prune --production \
    && npm cache clean --force

FROM node:10.13-alpine

WORKDIR /app

RUN npm config set registry https://registry.npm.taobao.org \
    && npm config set unsafe-perm true \
    && npm install -g supervisor
COPY --chown=node:node --from=build /src/server.js .
COPY --chown=node:node --from=build /src/dist dist/
COPY --chown=node:node --from=build /src/node_modules node_modules/

EXPOSE 6666
USER node

ENTRYPOINT [ "supervisor", "--harmony", "-w", "dist", "-e", "node,js,json", "server.js" ]
